﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// 主要用于对登入的手机UserAgent信息进行分析的帮助类
/// </summary>
public class UserAgent
{
	#region 变量定义
    private HttpRequest _request = null;
    #endregion

    #region  构造函数
    protected UserAgent(HttpRequest Request)
    {
        _request = Request;
    }
    public static UserAgent GetUserAgent()
    {
        return new UserAgent(HttpContext.Current.Request);
    }
    public static UserAgent GetUserAgent(HttpRequest Request)
    {
        return new UserAgent(Request);
    }
    #endregion

    #region 电话号码
    private string _phoneNum;
    /// <summary>
    /// 电话号码
    /// </summary>
    public string PhoneNum
    {
        get
        {
            string _phoneNum = _request.ServerVariables["HTTP_x_up_subno"];
            if (_phoneNum != null)
            {
                _phoneNum = _phoneNum.Substring(2, 11);
            }
            else
            {
                _phoneNum = _request.ServerVariables["HTTP_X_UP_CALLING_LINE_ID"];
            }
            if (_phoneNum == null)
            {
                _phoneNum = string.Empty;
            }
            return _phoneNum;
        }
    }
    #endregion

    #region 设备名称
    /// <summary>
    /// 设备名称
    /// </summary>
    public string DeviceName
    {
        get { 
            return _request.UserAgent.ToString();
        }
    }
    #endregion

    #region 设备屏幕宽度
    private int _deviceScreenWidth = 0;
    /// <summary>
    /// 屏幕宽度
    /// </summary>
    public int DeviceScreenWidth
    {
        get
        {
            _deviceScreenWidth = _request.Browser.ScreenCharactersWidth;
            return _deviceScreenWidth;
        }
    }
    #endregion

    #region 设备屏幕高度
    private int _deviceScreenHeight = 0;
    /// <summary>
    /// 屏幕高度
    /// </summary>
    public int DeviceScreenHeight
    {
        get
        {
            _deviceScreenHeight = _request.Browser.ScreenCharactersHeight;
            return _deviceScreenHeight;
        }
    }
    #endregion

    #region IP地址
    private string _ipAddress = "127.0.0.1";
    /// <summary>
    /// IP地址
    /// </summary>
    public string IpAddress
    {
        get {
            _ipAddress = _request.ServerVariables["REMOTE_ADDR"].ToString();
            if (_ipAddress == null || _ipAddress == "::1")
            {
                return "127.0.0.1";
            }
            return _ipAddress;
        }
    }
    #endregion
}
